import { ref, reactive } from "vue";
import { userLogin, userRegister } from '@/api/user'
import md5 from 'md5'
import { useRouter } from 'vue-router'
// this.$router.push('/home')

const userFormRules = (data, rulesData) => {
  const ruleForm = reactive(data);
  const rules = reactive(rulesData);
  // 路由实例
  const router = useRouter()
  // 提交验证
  const submitForm = (formEl) => {
    if (!formEl) return;
    formEl.validate((valid) => {
      if (valid) {
          userLogin({
            userName: ruleForm.userName,
            passWord: md5(ruleForm.passWord)
          }).then(res => {
              sessionStorage.setItem("token", res.data.token)
              if (res.data.code === 1) {
                router.push('/home')
              }
          })

        // 注册
        // userRegister(ruleForm).then(res => {
        //   console.log(res)
        // })
      } else {
        return false;
      }
    });
  };

  return {
    ruleForm,
    rules,
    submitForm
  };
};

export { userFormRules };
